<?php

/**
 * 默认接口服务类
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class Api_Doctor extends PhalApi_Api
{

    public function getRules()
    {
        return array(
            'login' => array(
                'username' => array('name' => 'username', 'require' => true),
                'password' => array('name' => 'password', 'require' => true, 'min' => 6),
                'ssid' => array('name' => 'SSID'),
                'device' => array('name' => 'Device', 'require' => true, 'max' => 1, 'min' => 1)//0 ios 1 android
            ),
            'getDoctorInfo' => array(
                'doctor_id' => array('name' => 'DoctorId', 'require' => true)
            ),
            'signIn' => array(
                'username' => array('name' => 'username', 'require' => true, 'min' => 6, 'max' => 32),
                'password' => array('name' => 'password', 'require' => true, 'min' => 6, 'max' => 32),
                'ssid' => array('name' => 'SSID'),
                'device' => array('name' => 'Device', 'require' => true, 'max' => 1, 'min' => 1)//0 ios 1 android
            ),
            'listDoctors' => array(
                'visible' => array('name' => 'Visible', 'max' => 1),
                'status' => array('name' => 'Status', 'max' => 1),
                'depart' => array('name' => 'Depart'),
                'hospital' => array('name' => 'Hospital'),
            ),
            'setDoctorLocation' => array(
                'doctor_id' => array('name' => 'DoctorId', 'require' => true,),
                'lng' => array('name' => 'Lng', 'require' => true,),
                'lat' => array('name' => 'Lat', 'require' => true,),
            ),
            'getDoctorLocation' => array(
                'doctor_id' => array('name' => 'DoctorId', 'require' => true,),
            ),
            'setDoctorStatus' => array(
                'doctor_id' => array('name' => 'DoctorId', 'require' => true,),
                'status' => array('name' => 'Status', 'require' => true,),
            ),
            'getDoctorStatus' => array(
                'doctor_id' => array('name' => 'DoctorId', 'require' => true,),
            ),
            'setDoctorVisible' => array(
                'doctor_id' => array('name' => 'DoctorId', 'require' => true,),
                'visible' => array('name' => 'Visible', 'require' => true,),
            ),
            'getDoctorVisible' => array(
                'doctor_id' => array('name' => 'DoctorId', 'require' => true,),
            ),
            'getEventInfo' => array(
                'event_id' => array('name' => 'EventId', 'require' => true)
            ),
            'getTest' => array(),
            'listDoctorWithDistance' => array(
                'lng' => array('name' => 'Lng', 'require' => true,),
                'lat' => array('name' => 'Lat', 'require' => true,)

            ),
            'setDoctorInfo' => array(
                'doctor_id' => array('name' => 'DoctorId', 'require' => true,),
                'name' => array('name' => 'Name'),
                'gender' => array('name' => 'Gender', 'max' => 4),
                'birth_date' => array('name' => 'BirthDate',),
                'doctor_card' => array('name' => 'DoctorCard',),
                'hospital' => array('name' => 'Hospital',),
                'depart' => array('name' => 'Depart',),
                'phone_number' => array('name' => 'PhoneNumber',),
                'IDCard' => array('name' => 'IDCard',)
            ),
            'upload' => array(
                'file' => array(
                    'name' => 'file',
                    'type' => 'file',
                    'min' => 0,
                    'max' => 1024 * 1024,
                    'range' => array('image/jpg', 'image/jpeg', 'image/png'),
                    'ext' => array('jpg', 'jpeg', 'png')
                ),
            ),
            'responseCall' => array(
                'response_id' => array('name' => 'ResponseId', 'require' => true),

            ),
            'catchDispatch' => array(
                'dispatch_id' => array('name' => 'DispatchId', 'require' => true),
            ),
            'arriveDispatch' => array(
                'dispatch_id' => array('name' => 'DispatchId', 'require' => true),
                'lng'=>array('name'=>'Lng','require'=>true),
                'lat'=>array('name'=>'Lat','require'=>true),
            ),
            'arriveWork' => array(
                'work_id' => array('name' => 'WorkId', 'require' => true),
                'lng'=>array('name'=>'Lng','require'=>true),
                'lat'=>array('name'=>'Lat','require'=>true),
            ),
            'setHandle' => array(
                'doctor_id' => array('name' => 'DoctorId'),
                'event_id' => array('name' => 'EventId'),
                'handle' => array('name' => 'Handle')
            ),
            'getHandle' => array(
                'handle_id' => array('name' => 'HandleId')
            ),
            'listHandle' => array(
                'event_id' => array('name' => 'EventId', 'require' => true)
            )

        );
    }

    /**
     * 医生登陆
     * @desc 医生登陆
     * @return string id 医生用户id
     * @return string msg 提示信息
     */
    public function login()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('id' => 0));

        $domain = new Domain_Doctor();
        $info = $domain->login($this->username, $this->password, $this->ssid, $this->device);
        // var_dump($info);

        if (empty($info)) {
            DI()->logger->debug('user not found', $this->username);

            $rs['code'] = 1;
            $rs['msg'] = T('用户名或密码错误');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of login', $rs);

        return $rs;
    }

    /**
     * 获取医生信息
     * @desc 获取医生信息
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function getDoctorInfo()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->getDoctorInfo($this->doctor_id);

        if (empty($info)) {
            DI()->logger->debug('', $this->doctor_id);

            $rs['code'] = 1;
            $rs['msg'] = T('不存在的医生');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of getDoctorInfo', $rs);

        return $rs;

    }

    /**
     *  医生注册
     * @desc 医生注册账号
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return object info string username 用户名
     * @return string msg 提示信息
     */
    public function signIn()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->signIn($this->username, $this->password,$this->ssid,$this->device);
        // var_dump($info);
        if (empty($info)) {
            DI()->logger->debug('', $this->username);

            $rs['code'] = 1;
            $rs['msg'] = T('同户名已存在');
            return $rs;
        }
        //var_dump($info);


        $rs['info'] = $info;
        DI()->logger->debug('info of sign in', $rs);

        return $rs;

    }

    /**
     * 获取医生列表
     * @desc 描述
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string id    医生id
     * @return string Name     姓名
     * @return string Hospital    医院
     * @return string Depart    科室
     * @return string Status    状态
     * @return string Location    地点
     * @return string msg 提示信息
     */
    public function listDoctors()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->listDoctors($this->visible, $this->status, $this->depart, $this->hospital);

        if (empty($info)) {
            DI()->logger->debug('no doctor');

            $rs['code'] = 1;
            $rs['msg'] = T('没有符合要求的医生');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of login', $rs);

        return $rs;

    }

    /**
     * 写入医生位置信息
     * @desc 写入医生位置信息
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象 true 成功 false 失败
     * @return string msg 提示信息
     */
    public function setDoctorLocation()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->setDoctorLocation($this->doctor_id, $this->lng, $this->lat);

        if (empty($info)) {
            DI()->logger->debug('update doctor`s location fail', array($this->doctor_id));

            $rs['code'] = 1;
            $rs['msg'] = T('update doctor`s location fail');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of setDoctorLocation', $rs);

        return $rs;

    }

    /**
     * 读取医生位置信息
     * @desc 读取医生位置信息
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return object info Location 位置信息经纬度
     * @return string msg 提示信息
     */
    public function getDoctorLocation()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->getDoctorLocation($this->doctor_id);

        if (empty($info)) {
            DI()->logger->debug('get doctor location fail', $this->doctor_id);

            $rs['code'] = 1;
            $rs['msg'] = T('doctor not exists');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info get doctor location', $rs);

        return $rs;

    }

    /**
     * 写入医生状态信息
     * @desc 写入医生位置信息
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象 true 成功 false 失败
     * @return string msg 提示信息
     */
    public function setDoctorStatus()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->setDoctorStatus($this->doctor_id, $this->status);

        if (empty($info)) {
            DI()->logger->debug('update doctor`s status fail', array($this->doctor_id));

            $rs['code'] = 1;
            $rs['msg'] = T('update doctor`status fail');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of setDoctorStatus', $rs);

        return $rs;

    }

    /**
     * 读取医生状态信息
     * @desc 读取医生状态信息
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return object info Status 状态信息
     * @return string msg 提示信息
     */
    public function getDoctorStatus()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->getDoctorStatus($this->doctor_id);

        if (empty($info)) {
            DI()->logger->debug('get doctor Status fail', $this->doctor_id);

            $rs['code'] = 1;
            $rs['msg'] = T('doctor not exists');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info get doctor Status', $rs);

        return $rs;

    }

    public function setDoctorVisible()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->setDoctorVisible($this->doctor_id, $this->visible);

        if (empty($info)) {
            DI()->logger->debug('update doctor`s visible fail', array($this->doctor_id));

            $rs['code'] = 1;
            $rs['msg'] = T('update doctor`visible fail');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of setDoctorVisible', $rs);

        return $rs;

    }

    /**
     * 读取医生状态信息
     * @desc 读取医生状态信息
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return object info Status 状态信息
     * @return string msg 提示信息
     */
    public function getDoctorVisible()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->getDoctorVisible($this->doctor_id);

        if (empty($info)) {
            DI()->logger->debug('get doctor visible fail', $this->doctor_id);

            $rs['code'] = 1;
            $rs['msg'] = T('get doctor visible fail');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info get doctor visible', $rs);

        return $rs;

    }

    /**
     *测试距离计算
     * @desc
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function getTest()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_PushIos();


        $info = $domain->pushMessageToList();

        if (empty($info)) {
            DI()->logger->debug('', '');

            $rs['code'] = 1;
            $rs['msg'] = T('失败');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of login', $rs);

        return $rs;

    }

    /**
     * 按距离获取医生列表
     * @desc 按距离获取医生列表
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function listDoctorWithDistance()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->listDoctorWithDistance($this->lng, $this->lat);

        if (empty($info)) {
            DI()->logger->debug('getDoctorWithDistance()', '');

            $rs['code'] = 1;
            $rs['msg'] = T('没有医生');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of login', $rs);

        return $rs;

    }

    /**
     * 修改医生信息
     * @desc 修改医生信息
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function setDoctorInfo()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->setDoctorInfo(
            $this->doctor_id,
            $this->name,
            $this->gender,
            $this->birth_date,
            $this->doctor_card,
            $this->hospital,
            $this->depart,
            $this->phone_number,
            $this->IDCard
        );

        if (empty($info)) {
            DI()->logger->debug('setDoctorInfo()', '');

            $rs['code'] = 1;
            $rs['msg'] = T('修改医生信息失败');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of login', $rs);

        return $rs;

    }

    /**
     * 医生用查看事件
     * @desc 医生查看事件
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function getEventInfo()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->getEventInfo($this->event_id);

        if (empty($info)) {
            DI()->logger->debug('getEventInfo()', '');

            $rs['code'] = 1;
            $rs['msg'] = T('医生查看事件失败');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of getEventInfo()', $rs);

        return $rs;

    }

    /**
     * 上传文件
     * @return string $url 绝对路径
     * @return string $file 相对路径，用于保存至数据库，按项目情况自己决定吧
     */
    public function upload()
    {

        $domain = new Domain_Doctor();
        $domain->upload($this->file);
        $rs = $_FILES["file"]["size"];

        return $rs;
    }

    /**
     * 响应请求
     * @desc 医生响应请求
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function responseCall()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array());

        $domain = new Domain_Doctor();
        $info = $domain->responseCall($this->response_id);

        if (empty($info)) {
            DI()->logger->debug('responseCall()', '');

            $rs['code'] = 1;
            $rs['msg'] = T('医生响应请求失败');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of responseCall()', $rs);

        return $rs;

    }

    /**
     * 立即出诊
     * @desc 立即出诊
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function catchDispatch()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->catchDispatch($this->dispatch_id, 1);

        if (empty($info)) {
            DI()->logger->debug('catchDispatch()', '');

            $rs['code'] = 1;
            $rs['msg'] = T('立即出诊失败');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of catchDispatch()', $rs);

        return $rs;

    }

    /**
     * 到达位置
     * @desc 到达位置上报 Status =2
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function arriveDispatch()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->arriveDispatch($this->dispatch_id, 2,$this->lng,$this->lat);

        if (empty($info)) {
            DI()->logger->debug('arriveDispatch()', '');

            $rs['code'] = 1;
            $rs['msg'] = T('到达位置上报失败');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of arriveDispatch()', $rs);

        return $rs;

    }
    /**
     * 到达位置 work
     * @desc 到达位置上报 Status =2
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function arriveWork()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->arriveWork($this->work_id, 1,$this->lng,$this->lat);

        if (empty($info)) {
            DI()->logger->debug('arriveWork()', '');

            $rs['code'] = 1;
            $rs['msg'] = T('到达位置上报失败');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of arriveWork()', $rs);

        return $rs;

    }

    /**
     * 现场处理
     * @desc 现场为了急救采取的措施，1请求车辆，2请求急救包，3请求知道4大家
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function setHandle()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->setHandle($this->event_id, $this->doctor_id, $this->handle);

        if (empty($info)) {
            DI()->logger->debug('setHandel()', '');

            $rs['code'] = 1;
            $rs['msg'] = T('现场处理失败');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of setHandel()', $rs);

        return $rs;

    }

    /**
     * 获取处理内容
     * @desc 获取已经创建的处理内容
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function getHandle()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->getHandle($this->handle_id);

        if (empty($info)) {
            DI()->logger->debug('getHandle()', '');

            $rs['code'] = 1;
            $rs['msg'] = T('获取处理内容失败');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of getHandle()', $rs);

        return $rs;

    }

    /**
     * 获取处理列表
     * @desc 获取handle的列表
     * @return int code 操作码，0表示成功， 1错误
     * @return object info 信息对象
     * @return string msg 提示信息
     */
    public function listHandle()
    {
        $rs = array('code' => 0, 'msg' => '', 'info' => array('a' => 1));

        $domain = new Domain_Doctor();
        $info = $domain->listHandle($this->event_id);

        if (empty($info)) {
            DI()->logger->debug('listHandle()', '');

            $rs['code'] = 1;
            $rs['msg'] = T('没有处理信息');
            return $rs;
        }

        $rs['info'] = $info;
        DI()->logger->debug('info of listHandle()', $rs);

        return $rs;

    }


}